﻿using System;
using System.Net;
using System.ServiceModel;
using System.Threading;
using System.Xml.Serialization;

namespace Galaktika.BI.Silverlight
{
    public static class ServiceManager
    {
        public static T CreateService<T, Base>()
            where T : ClientBase<Base>, Base, new()
            where Base : class
        {
            T client = new T();
            InitUrl<T, Base>(client);
            InitTimeout<T, Base>(client);

            return client;
        }

        //[Obsolete("Не использовать!")]
        private static void InitUrl<T, Base>(T client)
            where T : ClientBase<Base>, Base, new()
            where Base : class
        {
            if (m_Settings != null && !string.IsNullOrEmpty(m_Settings.BaseUri))
            {
                try
                {
                    Uri uri = client.Endpoint.Address.Uri;
                    string srvName = uri.LocalPath.Remove(0, 1);
                    int slashIndex = srvName.LastIndexOf('/');
                    if (slashIndex > 0)
                    {
                        srvName = srvName.Substring(slashIndex + 1, srvName.Length - slashIndex - 1);
                    }
                    client.Endpoint.Address = new EndpointAddress(new Uri(new Uri(m_Settings.BaseUri), srvName));
                }
                catch (Exception exc)
                {
                    // TODO: User service container
                    //App.CurrentApp.LogError(exc);
                }
            }
        }

        private static void InitTimeout<T, Base>(T client)
            where T : ClientBase<Base>, Base, new()
            where Base : class
        {
            if (m_Settings != null && !string.IsNullOrEmpty(m_Settings.Timeout))
            {
                TimeSpan timeout;
                if (TimeSpan.TryParse(m_Settings.Timeout, out timeout))
                {
                    client.InnerChannel.OperationTimeout = timeout;
                }
            }
        }


        public static void Initialize(Settings settings)
        {
            m_Settings = settings;
        }

        static Settings m_Settings;
    }

    [XmlRoot(Namespace = Common.Namespace)]
    public class Settings
    {
        public Settings()
        {
            this.MaxUserTaskThreads = 2;
            this.DataAccessMode = DataAccessMode.Default;
        }

        [XmlElement]
        public string BaseUri { get; set; }

        [XmlElement]
        public string Timeout { get; set; }

        [XmlElement]
        public bool EnableClientCaching { get; set; }

        [XmlElement]
        public bool EnableLogging { get; set; }

        [XmlElement]
        public bool EnableDebugging { get; set; }

        [XmlElement]
        public int MaxUserTaskThreads { get; set; }

        [XmlElement]
        public DataAccessMode DataAccessMode { get; set; }
    }

    public enum DataAccessMode
    {
        Deffered,
        Immediate,
        Default = Deffered,
        Unknown
    }
}
